/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package xin.alum.aim.model;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import lombok.Data;
import lombok.SneakyThrows;
import xin.alum.aim.constant.AIMConstant;
import xin.alum.aim.model.proto.Packet;

/**
 * 请求应答对象
 */
@Data
public class Reply extends DBase {

    /**
     * 返回码
     */
    private int code = 0;

    /**
     * 返回说明
     */
    private String message = "操作成功";

    @Override
    public int getType() {
        return AIMConstant.DATA_TYPE_REPLY;
    }

    public Reply() {
    }

    public Reply(String key) {
        super(key);
    }

    public Reply(String key, String message) {
        super(key);
        this.message = message;
    }

    public Reply(String key, long timestamp) {
        super(key, timestamp);
    }

    public Reply(String key, String message, Any data) {
        super(key);
        this.message = message;
        this.setData(data);
    }

    public Reply(String key, long timestamp, int code, String message, Any data) {
        super(key, timestamp);
        this.code = code;
        this.message = message;
        this.setData(data);
    }

    /**
     * 单例对象
     *
     * @param key
     * @return
     */
    public static Reply instance(String key) {
        Reply r = new Reply(key);
        return r;
    }

    /**
     * 单例对象
     *
     * @param key
     * @param msg
     * @return
     */
    public static Reply instance(String key, String msg) {
        Reply r = instance(key);
        r.setMessage(msg);
        return r;
    }

    /**
     * 单例对象
     *
     * @param key
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T extends Any> Reply instance(String key, String msg, T data) {
        Reply r = instance(key, msg);
        r.setData(data);
        return r;
    }

    /**
     * proto 对象转换
     *
     * @return
     */

    @Override
    public Packet.Reply toBuilder() {
        Packet.Reply.Builder builder = Packet.Reply.newBuilder();
        builder.setKey(super.getKey());
        builder.setMessage(message);
        builder.setCode(code);
        builder.setTimestamp(super.getTimestamp());
        if (data != null) {
            builder.setData(Any.pack(data));
        }
        return builder.build();
    }

    @SneakyThrows
    public String toString() {
        return toJson();
    }
}
